package com.zzg.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;


import com.zzg.entity.Page;
import com.zzg.entity.inform;
import com.zzg.service.InformService;


@Controller
@RequestMapping(value="/inform")
public class informController {
	InformService informService = new InformService();
	
	@RequestMapping(value="/list")
	public ModelAndView list(Integer currPage,String soukey,String aname,String role){
		ModelAndView mv=new ModelAndView();
		String key="%%";
		if(soukey!=null){
			key="%"+soukey+"%";
		}
		
		//分页
		int pageSize=5;
		if(currPage==null||currPage==0){
			currPage=1;
		}
		int index=(currPage-1)*pageSize;
		System.out.println(role);
		int totalCount=Integer.parseInt(informService.findCount(key,aname,role).get(0).get("count(id)").toString());
//		System.out.println(totalCount);
		Page page=new Page();
		page.setPageSize(pageSize);
		page.setTotalCount(totalCount);
		page.setCurrPage(currPage);
		mv.addObject("page", page);
		
		
		
		List<Map<String,Object>> list=informService.findAll(index,pageSize,key,aname,role);
//		System.out.println(list);
		mv.addObject("list", list);
		mv.addObject("key",soukey);
		mv.setViewName("inform_list");
		return mv;
	}
	
	@RequestMapping(value="/getAname")
	public ModelAndView getAname(HttpServletRequest request,String soukey){
		HttpSession session =  request.getSession();
		String aname = (String) session.getAttribute("username");
		String role = (String) session.getAttribute("role");
		System.out.print(aname);
		return list(1,soukey,aname,role);
	}
	
	@RequestMapping(value="/in")
	public ModelAndView add(Integer id,String role,String ititle,String icontent,HttpServletRequest request,Date iDate){
		HttpSession session =  request.getSession();
		String aname = (String) session.getAttribute("username");
		String Arole = (String) session.getAttribute("role");
		System.out.println(role);
		inform inform = new inform();
		inform.setId(id);
		inform.setRole(role);
		inform.setItitle(ititle);
		inform.setIcontent(icontent);
		inform.setAname(aname);
		System.out.print(aname);
		//inform.setiDate(iDate);
		System.out.print(inform);
		if(inform.getId()!=null) {
			informService.delById(id.toString());
		}
		informService.add(inform);

		return list(1,null,aname,Arole);
	}
	@RequestMapping(value="/goInform")
	public ModelAndView goEdit(Integer id){
		List<Map<String,Object>> list=null;
		ModelAndView mv=new ModelAndView();
		list= informService.findById(id);
		//System.out.print("却是突然一条如啼眼");
		mv.addObject("list",list);
		System.out.print(list);
		mv.setViewName("inform");
		return mv;
	}
	
	@RequestMapping(value="/show")
	public ModelAndView show(String id,Integer currPage){
//		System.out.println(id);
		ModelAndView mv = new ModelAndView();
		
		List<Map<String,Object>> list=informService.findById(id);
		mv.addObject("list", list);
		mv.setViewName("inform_show");
		return mv;
	}
	
	@RequestMapping(value="/del")
	public ModelAndView del(String id,HttpServletRequest request){
		HttpSession session =  request.getSession();
		String aname = (String) session.getAttribute("username");
		String role = (String) session.getAttribute("role");
		informService.delById(id);
		return list(1,null,aname,role);
	}
	//批量删除
	@RequestMapping(value="/delMore")
	public ModelAndView deleteMore(String[] sel,HttpServletRequest request)  {
		HttpSession session =  request.getSession();
		String aname = (String) session.getAttribute("username");
		String role = (String) session.getAttribute("role");
		informService.deleteMore(sel);
		return list(1,null,aname,role);
	}
}
